package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Business;
import cn.wolfcode.enums.BusinessReportGroupTypeEnum;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.qo.BusinessReportQueryObject;
import cn.wolfcode.service.IBusinessReportService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.RequirePermission;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/businessReport")
public class BusinessReportController {

    @Autowired
    private IBusinessReportService businessReportService;
    @Autowired
    private IBusinessService businessService;

    @RequirePermission(name = "门店报表页面",expression = "businessReport:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")BusinessReportQueryObject qo){
        PageInfo<HashMap> pageInfo = businessReportService.selectByType(qo);
        model.addAttribute("pageInfo", pageInfo);

        ConsumptionStatusEnum[] enums = ConsumptionStatusEnum.values();
        model.addAttribute("enums",enums);

        BusinessReportGroupTypeEnum[] values = BusinessReportGroupTypeEnum.values();
        model.addAttribute("values",values);

        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses",businesses);

        return "businessReport/list";
    }

    @RequirePermission(name = "门店柱状图报表",expression = "businessReport:listBar")
    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo")BusinessReportQueryObject qo){
        //为便于遍历,HashMap集合每个map里面相同的key同一存再另一个list中
        List<Object> groupTypeList = new ArrayList<>();//分组类型
        List<Object> totalCountList = new ArrayList<>();//总订单数
        List<Object> totalAmountList = new ArrayList<>();//总消费金额
        List<Object> discountAmountList = new ArrayList<>();//总优惠金额
        List<Object> payAmountList = new ArrayList<>();//总实收金额

        //查询所有结果(不分页,分页插件只要设置pageSize=0即不分页)
        qo.setPageSize(0);
        PageInfo<HashMap> pageInfo = businessReportService.selectByType(qo);
        //获取分页信息里的查询结果集合
        List<HashMap> list = pageInfo.getList();

        //遍历这个结果里面的每个元素,分别把每个map的对应key的值放在对应List中
        list.forEach(ele ->{
            groupTypeList.add(ele.get("groupType"));
            totalCountList.add(ele.get("totalCount"));
            totalAmountList.add(ele.get("totalAmount"));
            discountAmountList.add(ele.get("discountAmount"));
            payAmountList.add(ele.get("payAmount"));
        });

        //保存到model中
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("totalCountList",JSON.toJSONString(totalCountList));
        model.addAttribute("totalAmountList",JSON.toJSONString(totalAmountList));
        model.addAttribute("discountAmountList",JSON.toJSONString(discountAmountList));
        model.addAttribute("payAmountList",JSON.toJSONString(payAmountList));

        //共享标题,按什么分组
        String groupTypeName = BusinessReportGroupTypeEnum.findName(qo.getGroupType());
        model.addAttribute("groupTypeName",groupTypeName);
        return "businessReport/listBar";
    }
}
